cmake_minimum_required (VERSION 3.1)
#cmake_minimum_required (VERSION 3.3) ASAN need at least 3.3

# generate version.c
configure_file(
	${CMAKE_CURRENT_SOURCE_DIR}/version.c.in
	${CMAKE_BINARY_DIR}/version.c
	@ONLY
)

project(mod_srs)

set(CMAKE_EXPORT_COMPILE_COMMANDS on)
set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_BUILD_TYPE Release)
set(CMAKE_VERBOSE_MAKEFILE OFF)

# list of source files
set(libsrc mod_srs.c version.c srs_rtc.c)

IF(APPLE)
SET(CMAKE_SHARED_LIBRARY_SUFFIX ".so")
add_compile_options("$<$<CONFIG:Debug>:-O0>")
add_compile_options("$<$<CONFIG:Debug>:-g>")
ENDIF(APPLE)

include(GNUInstallDirs)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -I. -I/usr/local/include -L/usr/local/lib")
set(CMAKE_CPP_FLAGS "${CMAKE_CPP_FLAGS} -Wall -I. -I/usr/local/include -L/usr/local/lib")
set(CMAKE_POSITION_INDEPENDENT_CODE YES)

set(ENABLE_ASAN false)
IF(ENABLE_ASAN)
message("build using clang and ASAN")
set(CMAKE_C_COMPILER "/usr/bin/clang" CACHE string "clang compiler" FORCE)
set(CMAKE_CXX_COMPILER "/usr/bin/clang++" CACHE string "clang compiler" FORCE)
set(CMAKE_BUILD_TYPE ASAN)
set(CMAKE_XCODE_GENERATE_SCHEME ON)
set(CMAKE_XCODE_SCHEME_ADDRESS_SANITIZER ON)
set(CMAKE_XCODE_SCHEME_ADDRESS_SANITIZER_USE_AFTER_RETURN ON)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fno-omit-frame-pointer -fstack-protector-strong")
set(CMAKE_LD_FLAGS "${CMAKE_LD_FLAGS} -fsanitize=address")
ENDIF(ENABLE_ASAN)

find_package(PkgConfig)

set(ENV{PKG_CONFIG_PATH} "${CMAKE_INSTALL_PREFIX}/lib/pkgconfig/:/usr/local/freeswitch/lib/pkgconfig:/usr/local/lib:$ENV{PKG_CONFIG_PATH}")
message("PKG_CONFIG_PATH: $ENV{PKG_CONFIG_PATH}")

function(pkg_check_variable _pkg _name)
	string(TOUPPER ${_pkg} _pkg_upper)
	string(TOUPPER ${_name} _name_upper)
	string(REPLACE "-" "_" _pkg_upper ${_pkg_upper})
	string(REPLACE "-" "_" _name_upper ${_name_upper})
	set(_output_name "${_pkg_upper}_${_name_upper}")

	execute_process(COMMAND ${PKG_CONFIG_EXECUTABLE} --variable=${_name} ${_pkg}
					OUTPUT_VARIABLE _pkg_result
					OUTPUT_STRIP_TRAILING_WHITESPACE)

	set("${_output_name}" "${_pkg_result}" CACHE STRING "pkg-config variable ${_name} of ${_pkg}")
endfunction()

IF(ENABLE_LICENSE)
	set(XZZ_INCLUDE_DIR, "")
	pkg_check_modules (XZZ libxzz>=0.5)

	if (XZZ_FOUND)
		add_definitions(-DHAS_XZZ)
	endif()
ENDIF()


pkg_check_modules (FreeSWITCH REQUIRED freeswitch>=1.8)
pkg_check_variable(freeswitch modulesdir)
pkg_check_variable(freeswitch confdir)
message("FREESWITCH_MODULESDIR: ${FREESWITCH_MODULESDIR}")
message("LIBS: ${FreeSWITCH_LIBRARIES}")

link_directories(${FreeSWITCH_LIBRARY_DIRS})

# this is the "object library" target: compiles the sources only once
add_library(objlib OBJECT ${libsrc})
add_library(mod_srs SHARED $<TARGET_OBJECTS:objlib>)
set_target_properties(mod_srs PROPERTIES PREFIX "")
set_target_properties(mod_srs PROPERTIES DEBUG_POSTFIX "")
set_target_properties(mod_srs PROPERTIES CXX_VISIBILITY_PRESET hidden)
set_target_properties(mod_srs PROPERTIES C_VISIBILITY_PRESET hidden)
target_compile_definitions(mod_srs PRIVATE -DXZZ_API_VISIBILITY=0 -DXJSON_API_VISIBILITY=0)
add_dependencies(mod_srs version.c) # so it triggers the CUSTOM target and run on each build

if (KAFKA_FOUND)
	set(LIBS ${LIBS} ${KAFKA_LIBRARIES})
	message("kafka libs: ${LIBS}")
endif()

if (FreeSWITCH_FOUND)
	message("libfreeswitch found")
	include_directories(mod_srs ${FreeSWITCH_INCLUDE_DIRS} ${XZZ_INCLUDE_DIRS} ${KAFKA_INCLUDE_DIRS})
	set(LIBS ${LIBS} ${FreeSWITCH_LIBRARIES} ${XZZ_LIBRARIES})

	if(ENABLE_XNATS)
		set(LIBS ${LIBS} nats)
	endif()

	message("libs: ${LIBS}")

	target_link_libraries(mod_srs PRIVATE ${LIBS})
	target_compile_options(mod_srs PRIVATE ${FreeSWITCH_CFLAGS_OTHER})
endif()

install(FILES ${CMAKE_BINARY_DIR}/mod_srs.so DESTINATION ${FREESWITCH_MODULESDIR})

if (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
	add_custom_command(
		TARGET mod_srs POST_BUILD
		COMMAND
			# strip mod_srs.so
		COMMENT "Strip mod_srs")
endif()

add_custom_target(conf8
	COMMAND cp ./conf/autoload_configs/srs.conf.xml ${FREESWITCH_CONFDIR}/autoload_configs/)

add_custom_target(conf
	COMMAND cp ./conf/autoload_configs/srs.conf.xml ${FREESWITCH_CONFDIR}/configuration/)

add_custom_target(cclean
	COMMAND rm -rf CMakeCache.txt  CMakeFiles test/CMakeCache.txt test/CMakeFiles)

# add_custom_target(examples
# 	COMMAND tar cvzf srs-examples.tar.gz --exclude=examples/nodejs/node_modules examples)



add_custom_target(version.c ALL COMMAND bash ./gitversion.sh)

# enable_testing()
# add_subdirectory(test)
